//
//  AlbumViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "AlbumViewController.h"
#import "SongModel.h"
#import "DanquTableViewCell.h"

@interface AlbumViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic , strong)  UIImageView *backImg;
@property (nonatomic , strong)  UIView *tableHeaderView;
@property (nonatomic , strong)  UIImageView *portrait_pic;
@property (nonatomic , strong)  UILabel *singerName;
@property (nonatomic , strong)  UILabel *nameLabel;
@property (nonatomic , strong)  UILabel *descLabel;
@property (nonatomic , strong)  UIView *naviView;
@property (nonatomic , strong)  UILabel *naviLabel;
@property (nonatomic , strong)  UIView *headerView;
@property (nonatomic , strong)  UILabel *songCount;
@property (nonatomic , copy)  NSString *songCountStr;
@property (nonatomic , strong)  UIButton *storeBtn;
@property (nonatomic , strong)  UITableView *tableView;
@property (nonatomic , strong)  NSArray *songArr;
@property (nonatomic , strong) UIView *loadingView;

@property (nonatomic , copy)  NSString *picUrl;
@end

@implementation AlbumViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.naviView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 64)];
//    self.naviView.backgroundColor = [UIColor colorWithRed:55/255.0 green:95/255.0 blue:205/255.0 alpha:1];
    self.naviView.alpha = 0;
    self.naviLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, 44)];
    self.naviLabel.textAlignment = NSTextAlignmentCenter;
    self.naviLabel.textColor = [UIColor whiteColor];
    self.naviLabel.font = [UIFont systemFontOfSize:22];
    [self.naviView addSubview:self.naviLabel];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[[UIImage alloc] init] forBarMetrics:(UIBarMetricsDefault)];
    self.navigationController.navigationBar.clipsToBounds = YES;
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:(UIBarMetricsDefault)];
    self.navigationController.navigationBar.clipsToBounds = NO;
}
- (void)dataHandle
{
    NSString *str = [NSString stringWithFormat:@"http://api.dongting.com/song/album/%@",self.albumId];
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:str parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject[@"data"];
        self.nameLabel.text = dic[@"name"];
        self.naviLabel.text = dic[@"name"];
        if ([dic[@"description"] isKindOfClass:[NSString class]]) {
            NSArray *temp = [dic[@"description"] componentsSeparatedByString:@"/r/n"];
           self.descLabel.text = temp[0];
        }
        self.picUrl = dic[@"picUrl"];
        [self.backImg sd_setImageWithURL:[NSURL URLWithString:dic[@"picUrl"]]];
        self.singerName.text = dic[@"singerName"];
        if (!dic[@"singerPicUrl"]) {
            [self.portrait_pic sd_setImageWithURL:[NSURL URLWithString:dic[@"singerPicUrl"]]];
        }
        
        self.songCountStr = [NSString stringWithFormat:@"共%ld首歌曲",[dic[@"songs"] count]];
        NSArray *songArr = dic[@"songList"];
        self.songArr = [SongModel transformWithArray:songArr];
        [self.tableView reloadData];
       
        [self.loadingView removeFromSuperview];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
}

- (void)createBackImg{
    self.backImg = [[UIImageView alloc] initWithFrame:CGRectMake(0, -30, self.view.frame.size.width, 150 + 65 + 64 + 30 + 30)];
    //    _backImg.backgroundColor = [UIColor redColor];
    self.backImg.image = [UIImage imageNamed:@"lyric-sharing-background-2"];
    [self.view addSubview:self.backImg];
}
- (void)createTableView{
    [self createTableViewHeaderView];
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 - 70) style:(UITableViewStylePlain)];
    self.tableView.rowHeight = 60;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    UIView *tempView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150)];
    tempView.backgroundColor = [UIColor clearColor];
    self.tableView.tableHeaderView = tempView;
    [self.tableView registerClass:[DanquTableViewCell class] forCellReuseIdentifier:@"songlistCell"];
    [self.view addSubview:self.tableView];
    }

- (void)createTableViewHeaderView{
    self.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 150 + 64)];
    self.tableHeaderView.backgroundColor = [UIColor clearColor];
    
    self.portrait_pic = [[UIImageView alloc] init];
    self.portrait_pic.frame = CGRectMake(15, 64+5, 35, 35);
    self.portrait_pic.backgroundColor = [UIColor clearColor];
    self.portrait_pic.layer.cornerRadius = 35/2.0;
    self.portrait_pic.clipsToBounds = YES;
    [self.tableHeaderView addSubview:self.portrait_pic];
    
    self.singerName = [[UILabel alloc] initWithFrame:CGRectMake(self.portrait_pic.frame.origin.x + self.portrait_pic.frame.size.width + 5, 0, 100, 30)];
    self.singerName.center = CGPointMake(self.singerName.center.x, self.portrait_pic.center.y);
    self.singerName.font = [UIFont systemFontOfSize:14];
    self.singerName.textColor = [UIColor whiteColor];
    self.singerName.shadowOffset = CGSizeMake(1, 1);
    self.singerName.shadowColor = [UIColor blackColor];
    self.singerName.backgroundColor = [UIColor clearColor];
    [self.tableHeaderView addSubview:self.singerName];
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.portrait_pic.frame.origin.x , self.portrait_pic.frame.origin.y + self.portrait_pic.frame.size.height + 20, 200, 35)];
    self.nameLabel.font = [UIFont systemFontOfSize:20];
    self.nameLabel.textColor = [UIColor whiteColor];
    self.nameLabel.shadowOffset = CGSizeMake(1, 1);
    self.nameLabel.shadowColor = [UIColor blackColor];
    self.nameLabel.backgroundColor = [UIColor clearColor];
    [self.tableHeaderView addSubview:self.nameLabel];
    
    self.descLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.nameLabel.frame.origin.x, self.nameLabel.frame.origin.y + self.nameLabel.frame.size.height + 10, self.tableHeaderView.frame.size.width - self.nameLabel.frame.origin.x * 2, 35)];
    self.descLabel.font = [UIFont systemFontOfSize:14];
    self.descLabel.textColor = [UIColor whiteColor];
    self.descLabel.shadowOffset = CGSizeMake(1, 1);
    self.descLabel.shadowColor = [UIColor blackColor];
    self.descLabel.backgroundColor = [UIColor clearColor];
    [self.tableHeaderView addSubview:self.descLabel];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.songArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DanquTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"songlistCell" forIndexPath:indexPath];
    SongModel *songModel = _songArr[indexPath.row];
    cell.numLabel.text = [@(indexPath.row + 1) stringValue];
    cell.songName.text = songModel.name;
    cell.singerName.text = songModel.singerName;
    NSInteger count = [songModel.favorites integerValue];
    NSString *str = nil;
    if (count >= 10000) {
        CGFloat num = count / 10000.0;
        if (count % 10000 >= 1000) {
            str = [NSString stringWithFormat:@"%.1f 万",num];
        } else {
            str = [NSString stringWithFormat:@"%.0f 万",num];
        }
    } else {
        str = [NSString stringWithFormat:@"%ld",count];
    }
    cell.favorites.text = str;
    return cell;
}
 


@end
